package csc.fresher.spring.main;

import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import java.util.Scanner;

import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

import csc.fresher.spring.dao.EmployeeDAO;
import csc.fresher.spring.dao.EmployeeDAOImpl;
import csc.fresher.spring.domain.Employee;

public class MainApp {

	public static void main(String[] args) {
		ApplicationContext context = new ClassPathXmlApplicationContext(
				"Beans.xml");
		EmployeeDAO employeeDao = (EmployeeDAO) context
				.getBean("employeeDAOImpl");
		String query;
		System.out.println("My application support : select all, insert, select, delete, update");
		System.out.print("Please insert query: ");
		Scanner scanner = new Scanner(System.in);
		query = scanner.nextLine();
		if (query.equals("select all")) {
			List<Employee> list = employeeDao.listEmployee();
			for (Employee employee : list) {
				System.out.print("Id : " + employee.getId());
				System.out.print(", Name : " + employee.getName());
				System.out.print(", Age : " + employee.getAge());
				System.out.print(", Postion : " + employee.getPosition());
				System.out.print(", Phone : " + employee.getPhone());
				System.out.print(", Address : " + employee.getAddress());
				System.out.println(", Date Join : " + employee.getDateJoin());
			}
		} else if (query.equals("insert")) {
			System.out.print("Insert name:");
			String name = scanner.nextLine();
			System.out.print("Insert age:");
			String stringAge = scanner.nextLine();
			int age = Integer.parseInt(stringAge);
			System.out.print("Insert position:");
			String position = scanner.nextLine();
			System.out.print("Insert phone:");
			String phone = scanner.nextLine();
			System.out.print("Insert address:");
			String address = scanner.nextLine();
			System.out.print("Insert date join (yyyy-MM-dd):");
			String dateInsert = scanner.nextLine();

			DateFormat format = new SimpleDateFormat("yyyy-MM-dd");
			Date date = null;
			try {
				date = format.parse(dateInsert);
			} catch (ParseException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
			employeeDao.insertEmployee(name, age, position, phone, address,
					date);
		} else if (query.equals("select")) {
			System.out.print("Insert id of employee. Id is integer number: ");
			String stringId = scanner.nextLine();
			int id = Integer.parseInt(stringId);
			Employee employee = employeeDao.getEmployee(id);
			if(employee != null){
				System.out.print("Id : " + employee.getId());
				System.out.print(", Name : " + employee.getName());
				System.out.print(", Age : " + employee.getAge());
				System.out.print(", Postion : " + employee.getPosition());
				System.out.print(", Phone : " + employee.getPhone());
				System.out.print(", Address : " + employee.getAddress());
				System.out.println(", Date Join : " + employee.getDateJoin());
			}
		} else if (query.equals("delete")) {
			System.out.print("Insert id of employee. Id is integer number: ");
			String stringId = scanner.nextLine();
			int id = Integer.parseInt(stringId);
			employeeDao.delete(id);
		} else if (query.equals("update")) {
			System.out.print("Insert id employee you want update: ");
			String stringId = scanner.nextLine();
			int id = Integer.parseInt(stringId);
			System.out.print("Insert property: name, age, position, phone, address, date join of employee you want update: ");
			String property = scanner.nextLine();
			if(property.equals("name")){
				System.out.print("Insert new name: ");
				String name = scanner.nextLine();
				employeeDao.updateName(id, name);
			}
			if(property.equals("age")){
				System.out.print("Insert new age: ");
				String ageString = scanner.nextLine();
				int age = Integer.parseInt(ageString);
				employeeDao.updateAge(id, age);
			}
			if(property.equals("position")){
				System.out.print("Insert new position: ");
				String position = scanner.nextLine();
				employeeDao.updatePosition(id, position);
			}
			if(property.equals("phone")){
				System.out.print("Insert new phone: ");
				String phone = scanner.nextLine();
				employeeDao.updatePhone(id, phone);
			}
			if(property.equals("address")){
				System.out.print("Insert new address: ");
				String address = scanner.nextLine();
				employeeDao.updateAddress(id, address);
			}
			if(property.equals("date join")){
				System.out.print("Insert new date join (yyyy-MM-dd): ");
				String dateJoin = scanner.nextLine();
				DateFormat format = new SimpleDateFormat("yyyy-MM-dd");
				Date date = null;
				try {
					date = format.parse(dateJoin);
				} catch (ParseException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
				employeeDao.updateDateJoin(id, date);
			}
			
		} else {
			System.out.println("Error Syntax");
		}
		
	}

}
